package com.cloud.cloudshop.shop.controller;

import com.cloud.cloudshop.client.UidServiceClient;
import com.cloud.cloudshop.provider.CancelOrderSender;
import com.cloud.cloudshop.provider.DeadOrderSender;
import com.cloud.cloudshop.provider.RabbitCreateOrderAndSend;
import com.cloud.common.entity.GoodsEntity;
import com.cloud.common.entity.OrderEntity;
import com.cloud.common.utils.IDUtils;
import com.cloud.common.utils.UserResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/rabbitmq")
public class RabbitmqControllerTest {

    @Autowired
    private RabbitCreateOrderAndSend rabbitCreateOrderAndSend;

    @Autowired
    private UidServiceClient uidServiceClient;

    @Autowired
    private CancelOrderSender cancelOrderSender;

    @Autowired
    private DeadOrderSender deadOrderSender;




    /**
     * 用户下单系统
     *
     * @return
     */
    @GetMapping(value = "/createOrder",produces="application/json;charset=UTF-8")
    public UserResult test() {

        String s = uidServiceClient.UidGenerator();
        System.out.println(s);

        //发送消息测试
        OrderEntity order = new OrderEntity();
        order.setId(IDUtils.getStringId());
        order.setMessageId(IDUtils.getStringId());
        order.setName("create order");

        try {
            rabbitCreateOrderAndSend.createOrderByDirectExchange(order);

        } catch (Exception e) {
            log.error(e.getMessage());
            return UserResult.error(e.getMessage());
        }

        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setId("234234");


        return UserResult.success("下单成功");
    }


    /**
     * 直连队列
     *
     * @return
     */
    @GetMapping(value = "/direct",produces="application/json;charset=UTF-8")
    public UserResult direct() {
        //发送消息测试
        OrderEntity order = new OrderEntity();
        order.setId(IDUtils.getStringId());
        order.setMessageId(IDUtils.getStringId());
        order.setName("create order");
        log.info("mesid:"+order.getMessageId());
        try {
            rabbitCreateOrderAndSend.createOrderByDirectExchange(order);
        } catch (Exception e) {
            log.error(e.getMessage());
            return UserResult.error(e.getMessage());
        }

        return UserResult.success("直连消息发送成功。");
    }

    /**
     * 直连队列
     *
     * @return
     */
    @GetMapping("/topic")
    public UserResult topic() {
        //发送消息测试
        OrderEntity order = new OrderEntity();
        order.setId(IDUtils.getStringId());
        order.setMessageId(IDUtils.getStringId());
        order.setName("create order");
        log.info("mesid:"+order.getMessageId());
        try {
            rabbitCreateOrderAndSend.createOrderByTopicExchange(order);
        } catch (Exception e) {
            log.error(e.getMessage());
            return UserResult.error(e.getMessage());
        }

        return UserResult.success("主题消息发送成功。");
    }

    /**
     * 直连队列
     *
     * @return
     */
    @GetMapping("/fanout")
    public UserResult fanout() {
        //发送消息测试
        OrderEntity order = new OrderEntity();
        order.setId(IDUtils.getStringId());
        order.setMessageId(IDUtils.getStringId());
        order.setName("create order");
        log.info("mesid:"+order.getMessageId());
        try {
            rabbitCreateOrderAndSend.createOrderByFanoutExchange(order);
        } catch (Exception e) {
            log.error(e.getMessage());
            return UserResult.error(e.getMessage());
        }

        return UserResult.success("广播消息发送成功。");
    }

    /**
     * 延迟队列
     *
     * @return
     */
    @GetMapping("/deleyMeg")
    public UserResult deleyMeg() {

        long delayTimes = 30 * 1000;
        cancelOrderSender.sendMessage(1000000l,delayTimes);

        return UserResult.success("延迟消息发送成功。");
    }

    /**
     * 延迟队列
     *
     * @return
     */
    @GetMapping("/deadMeg")
    public UserResult deadMeg() {

        deadOrderSender.sendMessage(1000000l);

        return UserResult.success("dead消息发送成功。");
    }



}
